package com.sjzd.server.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sjzd.server.mapper.DepartmentMapper;
import com.sjzd.server.pojo.Department;
import com.sjzd.server.pojo.RespBean;
import com.sjzd.server.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sjzd
 * @since 2021-02-21
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public RespBean addDept(Department department) {
        department.setEnabled(true);
        departmentMapper.addDept(department);
        if (department.getResult() > 0) {
            return RespBean.success("添加成功！", department);
        }
        return RespBean.error("添加失败！");
    }

    @Override
    public RespBean delDeptById(int id) {
        Department department = new Department();
        department.setId(id);
        departmentMapper.delDeptById(department);
        if (department.getResult() == -2) {
            return RespBean.error("该部门下有子级部门，删除失败！");
        }
        if (department.getResult() == -1) {
            return RespBean.error("该部门下有员工，删除失败！");
        }
        if (department.getResult() == 1) {
            return RespBean.success("删除成功！");
        }
        return RespBean.success("删除失败！");
    }

    @Override
    public List<Department> getAllDepts() {
       return departmentMapper.getDeptsByPid(-1);
    }
}
